// ignore_for_file: file_names, must_be_immutable, constant_identifier_names, slash_for_doc_comments, avoid_unnecessary_containers, prefer_const_constructors, unnecessary_cast, sized_box_for_whitespace, prefer_function_declarations_over_variables, overridden_fields, annotate_overrides

import 'package:dismissible_page/dismissible_page.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:mybase/base/tool/arouter/FFGotoManager.dart';
import 'package:mybase/base/tool/pprint/FFLog.dart';
import 'package:page_transition/page_transition.dart';

/**跳转页面 */
class FFGoto {
  /**
   * 跳转到某个页面
   */
  static Future gotoPage(
    BuildContext context,
    Widget pageWidget, {
    bool fullscreenDialog = false,
    bool? ignore = false,
  }) async {
    bool result = await FFGotoManager().gotoNextPage(context, pageWidget, "", ignore: ignore);
    if (!result) {
      log_info("FFGoto.gotoPage gotoNextPage 不让跳转");
      return;
    }
    return Navigator.push(
        context,
        MaterialPageRoute(
          builder: (context) => pageWidget,
          fullscreenDialog: fullscreenDialog,
        ));
  }

  /**
   * 跳转到某个路由
   */
  static Future gotoRouterName(
    BuildContext context,
    String routerName, {
    Object? arguments,
    bool? ignore = false,
  }) async {
    bool result = await FFGotoManager().gotoNextPage(context, null, routerName, ignore: ignore);
    if (!result) {
      log_info("FFGoto.gotoPage gotoNextPage 不让跳转");
      return;
    }
    return Navigator.pushNamed(context, routerName, arguments: arguments);
  }

  /**
   * 切换页面，销毁当前的页面
   */
  static Future gotoReplacementNamed<T extends Object?, TO extends Object?>(
    BuildContext context,
    String routeName, {
    TO? result,
    Object? arguments,
    bool? ignore = false,
  }) async {
    bool result = await FFGotoManager().gotoNextPage(context, null, routeName, ignore: ignore);
    if (!result) {
      log_info("FFGoto.gotoPage gotoNextPage 不让跳转");
      return;
    }
    return Navigator.pushReplacementNamed(
      context,
      routeName,
      result: result,
      arguments: arguments,
    );
  }

  /**
   * 跳转并替换当前页面
   */
  static Future replacePage(
    BuildContext context,
    Widget pageWidget, {
    bool fullscreenDialog = false,
    bool? ignore = false,
  }) async {
    bool result = await FFGotoManager().gotoNextPage(context, pageWidget, "", ignore: ignore);
    if (!result) {
      log_info("FFGoto.gotoPage gotoNextPage 不让跳转");
      return;
    }
    return Navigator.pushReplacement(
        context, MaterialPageRoute(builder: (context) => pageWidget, fullscreenDialog: fullscreenDialog));
  }

  /**
   * 这个好用
   * 跳转页面 通过 插件 page_transition: ^2.1.0
   * isDismissible: 是否可以手势退出
   */
  static Future gotoPageByPlugin(
    BuildContext context,
    Widget pageWidget, {
    PageTransitionType type = PageTransitionType.bottomToTop,
    bool isDismissible = false,
    Duration? duration,
    Color backgroundColor = Colors.black,
    bool? ignore = false,
  }) async {
    bool result = await FFGotoManager().gotoNextPage(context, pageWidget, "", ignore: ignore);
    if (!result) {
      log_info("FFGoto.gotoPage gotoNextPage 不让跳转");
      return;
    }
    if (isDismissible) {
      pageWidget = DismissiblePage(
        onDismissed: () => Navigator.of(context).pop(),
        isFullScreen: true,
        disabled: false,
        minRadius: 10,
        maxRadius: 10,
        dragSensitivity: 1.0,
        maxTransformValue: .8,
        direction: DismissiblePageDismissDirection.down,
        backgroundColor: backgroundColor,
        onDragStart: () {},
        onDragUpdate: (details) {},
        dismissThresholds: const {
          DismissiblePageDismissDirection.vertical: .2,
        },
        minScale: .8,
        startingOpacity: 1,
        reverseDuration: const Duration(milliseconds: 250),
        // End of the optional properties
        child: pageWidget,
      );
    }

    return Navigator.push(
        context,
        PageTransition(
          type: type,
          child: pageWidget,
          duration: duration ?? const Duration(milliseconds: 250),
        ));
  }

  /**
   * 这个好用
   * 跳转页面 通过 插件 page_transition: ^2.1.0
   * isDismissible: 是否可以手势退出
   */
  @deprecated
  static Future gotoPageByPlugin2(
    BuildContext context,
    Widget pageWidget, {
    PageTransitionType type = PageTransitionType.bottomToTop,
    bool isDismissible = false,
    Duration? duration,
    Color backgroundColor = Colors.black,
    bool? ignore = false,
  }) async {
    bool result = await FFGotoManager().gotoNextPage(context, pageWidget, "", ignore: ignore);
    if (!result) {
      log_info("FFGoto.gotoPage gotoNextPage 不让跳转");
      return;
    }
    return Navigator.push(
        context,
        PageTransition(
          type: type,
          child: DismissiblePage(
            onDismissed: () => Navigator.of(context).pop(),
            // onDismissed: (){},
            isFullScreen: true,
            disabled: isDismissible,
            minRadius: 10,
            maxRadius: 10,
            dragSensitivity: 1.0,
            maxTransformValue: .8,
            direction: DismissiblePageDismissDirection.down,
            backgroundColor: backgroundColor,
            onDragStart: () {},
            onDragUpdate: (details) {},
            dismissThresholds: const {
              DismissiblePageDismissDirection.vertical: .2,
            },
            minScale: .8,
            startingOpacity: 1,
            reverseDuration: const Duration(milliseconds: 250),
            // End of the optional properties
            child: pageWidget,
          ),
          duration: duration ?? const Duration(milliseconds: 250),
        ));
  }

  /**
   * 这个好用
   * 跳转页面 通过 插件 page_transition: ^2.1.0
   * isDismissible: 是否可以手势退出
   */
  static Future gotoPageByPlugin3(
    BuildContext context,
    Widget pageWidget, {
    PageTransitionType type = PageTransitionType.bottomToTop,
    bool isDismissible = false,
    Duration? duration,
    Color backgroundColor = Colors.black,
    bool? ignore = false,
  }) async {
    bool result = await FFGotoManager().gotoNextPage(context, pageWidget, "", ignore: ignore);
    if (!result) {
      log_info("FFGoto.gotoPage gotoNextPage 不让跳转");
      return;
    }
    return Navigator.push(
        context,
        PageTransition(
          type: type,
          child: DismissiblePage(
            onDismissed: () => Navigator.of(context).pop(),
            isFullScreen: true,
            disabled: !isDismissible,
            minRadius: 10,
            maxRadius: 10,
            dragSensitivity: 1.0,
            maxTransformValue: .8,
            direction: DismissiblePageDismissDirection.down,
            backgroundColor: backgroundColor,
            onDragStart: () {},
            onDragUpdate: (details) {},
            dismissThresholds: const {
              DismissiblePageDismissDirection.vertical: .2,
            },
            minScale: .8,
            startingOpacity: 1,
            reverseDuration: const Duration(milliseconds: 250),
            // End of the optional properties
            child: pageWidget,
          ),
          duration: duration ?? const Duration(milliseconds: 250),
        ));
  }

  /**
   * 从下往上弹: 适用于小窗口
   * useSafeArea: 是否使用安全区域
   */
  @deprecated
  static Future<void> gotoPageByBodalBottom(
    BuildContext context,
    Widget pageWidget, {
    bool useSafeArea = false,
    bool? ignore = false,
  }) async {
    bool result = await FFGotoManager().gotoNextPage(context, pageWidget, "", ignore: ignore);
    if (!result) {
      log_info("FFGoto.gotoPage gotoNextPage 不让跳转");
      return;
    }
    await showModalBottomSheet(
      barrierColor: Colors.black54,
      context: context,
      isScrollControlled: true,
      backgroundColor: Colors.transparent,
      useSafeArea: useSafeArea,
      builder: (ctx) {
        return pageWidget;
      },
    );
  }

  /**
   * 从下往上弹 页面
   */
  static Future<void> gotoPageByModal(
    BuildContext context,
    Widget pageWidget, {
    Color barrierColor = kCupertinoModalBarrierColor,
    bool? ignore = true,
  }) async {
    bool result = await FFGotoManager().gotoNextPage(context, pageWidget, "", ignore: ignore);
    if (!result) {
      log_info("FFGoto.gotoPage gotoNextPage 不让跳转");
      return;
    }
    await showCupertinoModalPopup(
      context: context,
      barrierColor: barrierColor,
      builder: (BuildContext context) {
        return Dismissible(
          behavior: HitTestBehavior.opaque,
          key: GlobalKey(),
          direction: DismissDirection.down,
          child: pageWidget,
          onDismissed: (_) {
            FFGoto.pop(context);
          },
        );
      },
    );
  }

  /**
   * 能否返回
   */
  static bool canPop(BuildContext context) {
    return Navigator.canPop(context);
  }

  /**
   * 返回页面
   */
  static Future<void> pop(
    BuildContext context, {
    bool? isRoot = false,
    bool? ignore = false,
  }) async {
    if (!context.mounted) {
      return;
    }
    if (isRoot == true) {
      bool result = await FFGotoManager().gotoPreviousPage(context, ignore: ignore);
      if (!result) {
        log_info("FFGoto.pop gotoPreviousPage 不让跳转");
        return;
      }
      Navigator.of(context).popUntil((route) => route.isFirst);
    } else {
      if (Navigator.canPop(context)) {
        bool result = await FFGotoManager().gotoPreviousPage(context,ignore: ignore);
        if (!result) {
          log_info("FFGoto.pop gotoPreviousPage 不让跳转");
          return;
        }
        Navigator.pop(context);
      }
    }
  }
}
